<?php

	sleep(5);

	//重启脚本
	//$restart_cmd = "service rabbitmq-server restart";
    $restart_cmd = "/etc/init.d/rabbitmq-server restart &";

    //日志保存位置
    $log_path = '/home/zyyt/log/rabbitmq/get_mq.log';

	$conn_args = [
		'host' => '127.0.0.1',  //rabbitmq 服务器host
		'port' => 5672,         //rabbitmq 服务器端口
		'login' => 'guest',     //登录用户
		'password' => 'guest',   //登录密码
		'vhost' => '/'         //虚拟主机
	];

	$config = [
		'exchangeName' => 'uweibomatchv4',
		'eqeueName' => 'check_mq',
		'eqeueName_key' => 'check_mq_key'
	];

	$conn = new AMQPConnection($conn_args);
	$connect = $conn->connect();
	if (!$connect) {
		//执行重启脚本
		$msg =  date('Y-m-d H:i:s')." connect error  连接rabbitmq服务失败. \n";
		error_log($msg, 3, $log_path);
		exec($restart_cmd);
        return;
	}

	
	$channel = new AMQPChannel($conn);
	$q_name = $config['eqeueName'];


	$q = new AMQPQueue($channel);
	$q->setName($q_name);

	$arr = $q->get();
	if (!$arr){
		//执行重启脚本
		//echo "publish error \n";
        $msg =  date('Y-m-d H:i:s')." publish error  数据无法成功写入队列. \n";
        error_log($msg, 3, $log_path);
		exec($restart_cmd);
        return;
	}

	$ack = $arr->getDeliveryTag();
	$res = $q->ack($ack);
	$msg = $arr->getBody();
	
	if('123' == $msg){
		//echo "mq ok \n";
		//执行
        	$msg =  date('Y-m-d H:i:s')." mq ok. \n";
        	error_log($msg, 3, $log_path);
		return;
	}else{
		//执行重启脚本
		//echo "getBody error \n";
        $msg =  date('Y-m-d H:i:s')." getBody error  队列中的验证数据不正确. \n";
        error_log($msg, 3, $log_path);
		exec($restart_cmd);
        return;
	}

?>
